package org.apache.ddlutils;

import cn.hutool.db.ds.simple.SimpleDataSource;
import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.platform.mysql.MySqlPlatform;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.ByteArrayOutputStream;

public class Demo {

    private Database readDatabaseFromXML(String fileName) {
        return new DatabaseIO().read(fileName);
    }

    private Database readDatabase(DataSource dataSource) {
        Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
        return platform.readModelFromDatabase("wiki");
    }

    @Test
    public void sql() {
        Database database = readDatabaseFromXML("C:\\Users\\fjay\\Documents\\work\\vip\\code\\csm\\csm-workflow\\doc\\db\\tables.xml");
        Platform platform = PlatformFactory.createNewPlatformInstance(MySqlPlatform.DATABASENAME);
        System.out.println(platform.getCreateModelSql(database, true, false));
    }

    @Test
    public void xml() {
        DataSource ds = new SimpleDataSource("jdbc:mysql://10.198.194.102:3306/wiki", "root", "root");
        ByteArrayOutputStream o = new ByteArrayOutputStream();
        new DatabaseIO().write(readDatabase(ds), o);
        System.out.println(o.toString());
    }
}